import { imageLoadFileAsDataURL } from '@vimi/utils-tool'
import { service } from '@vimi/utils-api'
const APP_CODE = '84fcd2ba60154f70b7ede5106314b4fd'
const OCR_IDCARD_URL = '//dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json'
const OCR_LICENSE_URL = '//dm-58.data.aliyun.com/rest/160601/ocr/ocr_business_license.json'
export const OCR_LICENSE_PERIOD_LONG = '29991231'
export const getIdCard = async (file, face = true) => {
  const fileBase64 = await imageLoadFileAsDataURL(file)
  // if (fileBase64) {
  //   return Promise.resolve({
  //     data: {
  //       num: '522425199601160610',
  //       name: '陈玉',
  //       address: '毕节',
  //     },
  //   })
  // }
  if (!fileBase64) {
    return Promise.resolve()
  }
  return service.post(
    OCR_IDCARD_URL,
    {
      image: fileBase64,
      configure: {
        side: face ? 'face' : 'back',
      },
    },
    {
      headers: {
        Authorization: `APPCODE ${APP_CODE}`,
      },
    }
  )
}

export const getOrgInfo = async file => {
  const fileBase64 = await imageLoadFileAsDataURL(file)
  // if (fileBase64) {
  //   return Promise.resolve({
  //     data: {
  //       reg_num: '91520621MA6E0EYEXX',
  //       person: '陈玉',
  //       name: 'OCR测试公司',
  //       address: '铜仁',
  //       capital: '二百万元整', // 注册资本
  //       establish_date: '20211212', // 成立日期
  //       valid_period: '20220120', //有效期
  //       business: '养猪，养狗',
  //     },
  //   })
  // }
  if (!fileBase64) {
    return Promise.resolve()
  }
  return service.post(
    OCR_LICENSE_URL,
    {
      image: fileBase64,
    },
    {
      headers: {
        Authorization: `APPCODE ${APP_CODE}`,
      },
    }
  )
}

export const ocrDateFormat = date => {
  if (!date || date.length !== 8) {
    return ''
  }
  return `${date.substring(0, 4)}年${date.substring(4, 6)}月${date.substring(6)}日`
}
